home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Utilities ƒ / MPW Tools ƒ / MakeMake / Source / vector.c < prev   
Encoding:
C/C++ Source or Header  |  1986-08-13  |  2.0 KB  |  89 lines  |  [TEXT/MPS ]

  1. # include <CType.h>
  2.  
  3. # define EOS    '\0'
  4.  
  5. /*
  6.  * Given an input string containing words separated by whitespace,
  7.  * return a vector (an array of char pointers) containing each
  8.  * separate word.  Words enclosed in single or double quotes, or
  9.  * in angle brackets, may contain whitespace; but words must still
  10.  * be separated by whitespace.  If not, some words will have their
  11.  * first character truncated.  Words may not contain their delimiters.### MPW Shell - Execution of RunMake terminated.
  12.  
  13.  *
  14.  * The parameters are argv[], an empty vector to be
  15.  * filled in; argc, the size of argv[]; and s, the input string.
  16.  *
  17.  * The input string s is mangled by this routine.
  18.  *
  19.  * Returns the number of words seen.  This may be larger than argc;
  20.  * if so, only the first argc words are placed in the vector.
  21.  */
  22. vector (argc, argv, s)
  23. int argc;
  24. char *argv[];
  25. char *s;
  26.     {
  27.     int i = 0;    /* Index into argv[] */
  28.     char delim;    /* Word delimiter    */
  29.     
  30.     while (*s != EOS)
  31.         {
  32.         /* Skip whitespace */
  33.         while (isspace (*s))
  34.             s++;
  35.         if (*s == EOS)
  36.             break;
  37.             
  38.         /* Save a pointer to this word, if there's room */
  39.         if (i < argc)
  40.             argv[i] = s;
  41.         i++;
  42.         
  43.         /* Decide what delimits the word */
  44.         switch (*s)
  45.             {
  46.             case '\'':
  47.                 delim = '\''; break;
  48.             case '"':
  49.                 delim = '"'; break;
  50.             case '<':
  51.                 delim = '>'; break;
  52.             default:
  53.                 delim = EOS; break;
  54.             }
  55.         
  56.         /* Find the end of the word */
  57.         s++;
  58.         if (delim == EOS)
  59.             {
  60.             /* Word is delimited by whitespace */
  61.             while (!isspace (*s) && *s != EOS)
  62.                 s++;
  63.             }
  64.         else
  65.             {
  66.             /* Word is delimited by a special character */
  67.             while (*s != delim && *s != EOS)
  68.                 s++;
  69.             if (*s == delim)
  70.                 s++;
  71.             }
  72.         
  73.         /*
  74.          * Place EOS after the end of the word,
  75.          * to terminate it.  There's a bug here:
  76.          * if there is no whitespace following the word
  77.          * (for example, <word1>word2 has a regular word
  78.          * following one in angle brackets),
  79.          * the first char of the next word is trashed.
  80.          */
  81.         if (*s == EOS)
  82.             break;
  83.         *s = EOS;
  84.         s++;
  85.         }
  86.     
  87.     /* Done.  Return the number of words seen. */
  88.     return (i);
  89.     }